package com.httpDemo.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.httpDemo.filter.MyFilter;
import com.httpDemo.listener.MyListener;
import com.httpDemo.servlet.UserServlet;

/**
 * 注册web的三大组件：Servlet,Filter,Listener 在RegistrationBean的子类中可以找到三大组件的注册器：
 * ServletRegistrationBean、FilterRegistrationBean、ServletListenerRegistrationBean
 * 
 * @author 86173
 *
 */
@Configuration
public class WebObjectConfig {

	public static final String USER_SERVLET_NAME = "userServlet";
	public static final String MY_FILTER_NAME = "myFilter";
	public static final String MY_LISTENER_NAME = "myListener";

	/**
	 * 创建Servlet对象
	 * 
	 * @return
	 */
	@Bean(name = USER_SERVLET_NAME)
	public UserServlet userServlet() {
		return new UserServlet();
	}

	/**
	 * 注册Servlet
	 * 
	 * @return
	 */
	@Bean
	@ConditionalOnBean(value = UserServlet.class, name = USER_SERVLET_NAME)
	public ServletRegistrationBean<UserServlet> servletRegistrationBeanUserServlet(UserServlet userServlet) {
		// 创建一个注册器
		ServletRegistrationBean<UserServlet> registrationBean = new ServletRegistrationBean<>();
		// 注入Servlet
		registrationBean.setServlet(userServlet);
		// 注入url-patten
		registrationBean.addUrlMappings("/user.do", "/user.action");
		// 注入初始化参数，可设置多个
		registrationBean.addInitParameter("key", "servlet-value");
		// 设置启动时加载
		registrationBean.setLoadOnStartup(1);
		return registrationBean;
	}

	/**
	 * 创建Filter对象
	 * 
	 * @return
	 */
	@Bean(name = MY_FILTER_NAME)
	public MyFilter myFilter() {
		return new MyFilter();
	}

	/**
	 * 注册Filter
	 * 
	 * @param myFilter
	 * @return
	 */
	@Bean
	@ConditionalOnBean(value = MyFilter.class, name = MY_FILTER_NAME)
	public FilterRegistrationBean<MyFilter> filterRegistrationBeanMyFilter(MyFilter myFilter) {
		// 创建注册器
		FilterRegistrationBean<MyFilter> registrationBean = new FilterRegistrationBean<MyFilter>();
		// 注入Filter
		registrationBean.setFilter(myFilter);
		// 注入url-patten
		registrationBean.addUrlPatterns("/*");
		// 注入初始化参数，可设置多个
		registrationBean.addInitParameter("key", "filter-value");
		return registrationBean;
	}

	/**
	 * 创建Listener对象
	 * 
	 * @return
	 */
	@Bean(name = MY_LISTENER_NAME)
	public MyListener myListener() {
		return new MyListener();
	}

	/**
	 * 注册Listener
	 * 
	 * @param myListener
	 * @return
	 */
	@Bean
	@ConditionalOnBean(value = MyListener.class, name = MY_LISTENER_NAME)
	public ServletListenerRegistrationBean<MyListener> servletListenerRegistrationBeanMyListener(
			MyListener myListener) {
		// 创建注册器
		ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>();
		// 注入Listener
		registrationBean.setListener(myListener);
		return registrationBean;
	}
}
